using System;
using System.Collections.Generic;
using System.Text;
using nDeveloper.BO.Info;

namespace nDeveloper.Data
{
    public enum RelationType
    {
        OneToOne,
        OneToMany,
        Unknown
    }

    public class DataRelation : IComparable<DataRelation>
    {
        private string relationName;
        private string primaryKeyTableName;
        private string primaryKeyColumnName;
        private string foreignKeyTableName;
        private string foreignKeyColumnName;
        private RelationType relationType;

        # region Propriedades
        public string RelationName
        {
            get
            {
                return relationName;
            }
            set
            {
                relationName = value;
            }
        }

        public string PrimaryKeyTableName
        {
            get
            {
                return primaryKeyTableName;
            }
            set
            {
                primaryKeyTableName = value;
            }
        }

        public string PrimaryKeyColumnName
        {
            get
            {
                return primaryKeyColumnName;
            }
            set
            {
                primaryKeyColumnName = value;
            }
        }

        public string ForeignKeyTableName
        {
            get
            {
                return foreignKeyTableName;
            }
            set
            {
                foreignKeyTableName = value;
            }
        }

        public string ForeignKeyColumnName
        {
            get
            {
                return foreignKeyColumnName;
            }
            set
            {
                foreignKeyColumnName = value;
            }
        }

        public RelationType RelationType
        {
            get
            {
                return relationType;
            }
            set
            {
                relationType = value;
            }
        }
        # endregion

        # region Construtores
        public DataRelation() { }
        public DataRelation(string relationName)
        {
            this.relationName = relationName;
            this.relationType = RelationType.Unknown;
        }
        public DataRelation(SystemDataDictionaryInfoReferences relation)
        {
            this.relationName = relation.Name;

            string type = relation.Cardinality;
            if (type.Equals("1:1")) this.relationType = RelationType.OneToOne;
            if (type.Equals("1:N")) this.relationType = RelationType.OneToMany;
            else this.relationType = RelationType.Unknown;

            this.primaryKeyTableName = relation.ParentName;
            this.primaryKeyColumnName = relation.ParentField;

            this.foreignKeyTableName = relation.ChildName;
            this.foreignKeyColumnName = relation.ChildField;
        }
        # endregion

        #region IComparable<DataRelation> Members

        public int CompareTo(DataRelation other)
        {
            int compResult;

            if (((compResult = String.Compare(this.primaryKeyTableName, other.primaryKeyTableName, StringComparison.CurrentCultureIgnoreCase)) != 0) ||
                ((compResult = String.Compare(this.primaryKeyColumnName, other.primaryKeyColumnName, StringComparison.CurrentCultureIgnoreCase)) != 0) ||
                ((compResult = String.Compare(this.foreignKeyTableName, other.foreignKeyTableName, StringComparison.CurrentCultureIgnoreCase)) != 0) ||
                ((compResult = String.Compare(this.foreignKeyColumnName, other.foreignKeyColumnName, StringComparison.CurrentCultureIgnoreCase)) != 0))
            {
                return compResult;
            }
            else
                return Convert.ToInt32(this.relationType) - Convert.ToInt32(other.relationType);
        }

        #endregion

        public bool Equals(SystemDataDictionaryInfoReferences relation)
        {
            string type = relation.Cardinality;
            //if (type.Equals("1:1")) { if (this.relationType != RelationType.OneToOne) return false; }
            //else if (type.Equals("1:N")) { if (this.relationType != RelationType.OneToMany) return false; }
            //else if (this.relationType != RelationType.Unknown) return false;

            return (this.primaryKeyTableName == relation.ParentName &&
                this.primaryKeyColumnName == relation.ParentField &&
                this.foreignKeyTableName == relation.ChildName &&
                this.foreignKeyColumnName == relation.ChildField &&
                this.relationName.EndsWith(relation.Name));
        }
    }
}